'''
Created on Apr 3, 2012

@author: michal
'''

import utils.utils as utils
import logging
from db.oracledb import oracledb
from db.mssqldb import mssqldb
from regex.grep import grep
from regex.sed import sed
from shell.shell import shell


def runOracle(section):
    dicparam = utils.getSectionParameters(section)
    oracle = oracledb() 
    connection = oracle.getConnection(host=dicparam['host'], sid=dicparam['sid'], user=dicparam['user'], password=dicparam['password'])
    sqlList = utils.parseFileIntoSQLList(dicparam['sqlfile'])
    oracle.executeSQLList(connection, sqlList, resultFile=dicparam['saveresults'], fileDate = bool(eval((dicparam['gen_date']))) )
    connection.close()

def runMSSQL(section):
    dicparam = utils.getSectionParameters(section)
    mssql = mssqldb() 
    connection = mssql.getConnection(host=dicparam['host'], database=dicparam['database'], user=dicparam['user'], password=dicparam['password'])
    sqlList = utils.parseFileIntoSQLList(dicparam['sqlfile'])
    mssql.executeSQLList(connection, sqlList, resultFile=dicparam['saveresults'], fileDate = bool(eval((dicparam['gen_date']))) )
    connection.close()

def runOraBatch(section):
    dicparam = utils.getSectionParameters(section)
    oracle = oracledb() 
    connection = oracle.getConnection(host=dicparam['host'], sid=dicparam['sid'], user=dicparam['user'], password=dicparam['password'])
    oracle.executeSQLwithCSV(connection, dicparam['csvfile'], dicparam['sqlfile'], testbatch=bool(eval((dicparam['testbatch']))))
    connection.close()

def runMSbatch(section):
    dicparam = utils.getSectionParameters(section)
    mssql = mssqldb() 
    connection = mssql.getConnection(host=dicparam['host'], database=dicparam['database'], user=dicparam['user'], password=dicparam['password'])
    mssql.executeSQLwithCSV(connection, dicparam['csvfile'], dicparam['sqlfile'], testbatch=bool(eval((dicparam['testbatch']))))
    connection.close()
    
def runGrep(section):
    dicparam = utils.getSectionParameters(section)
    grepF = grep()
    grepF.grepFile(dicparam['input'], dicparam['output'], dicparam['csvregex'], dicparam['csvdilimiter'])

def runSed(section):
    dicparam = utils.getSectionParameters(section)
    sedF = sed()
    sedF.sedFile(dicparam['input'], dicparam['output'], dicparam['csvregex'], dicparam['csvdilimiter'])

def runShellCommand(section):
    dicparam = utils.getSectionParameters(section)
    process = shell()
    process.runCommand(dicparam['command'], dicparam['outputfile'], bool(eval((dicparam['use_shell']))))
    
    